/*
 * WordWrap.java
 *
 * Created on October 25, 2009, 7:14 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.nextel.ui.custom;

import com.nextel.util.lwuit.Font;
import pe.com.nextel.util.ArrayList;
import pe.com.nextel.util.List;

/** A class supporting word wrap for MIDP. */

public class WordWrap {

    Font font;
    int width;
    String texto;
    int pos;
    public List listaTexto ;

    /**
     * Initializes the WordWrap object with the given Font, the text string
     * to be wrapped, and the target width.
     *
     * @param font: The Font to be used to calculate the character widths.
     * @param txt: The text string to be wrapped.
     * @param width: The line width.
     */

    public WordWrap (Font font, String txt, int pwidth) {

	this.font = font;
	this.texto = txt;
	this.width = pwidth;
        this.listaTexto = new ArrayList( );
    }

    /**
     * returns the next line break position. If no text is left, -1 is returned.
     */

    public int next () {

	int i = pos;
	int len = texto.length ();

	if (pos >= len) return -1;

	int start = pos;

	while (true) {
	    while (i < len && texto.charAt (i) > ' ')
		i++;

	    int w = font.stringWidth (texto.substring (start, i));
	    if (pos == start) {
                if (w > width) {
                    while (font.stringWidth (texto.substring (start, --i)) >width)
                    { }
                    pos = i;
                    break;
                }
            }

            if (w <= width) pos = i;

	    if (w > width || i >= len || texto.charAt(i) == '\n') break;
	    i++;
	}

	return pos >= len ? pos : ++pos;
    }




    public int numeroLineas () {
        int cont = 0;
        while (true){
            int np = next();
            if (np == -1) {
                break;
            } else {
                cont++;
            }
        }
        pos = 0;
        return cont;
    }


    public void dividirPalabraSinSalto( ) {

        int anchoCaracter = font.stringWidth("E");


        int numeroCarateresMostrar = width / (anchoCaracter);

        String textoTemporal = texto;
        int indiceCorte = numeroCarateresMostrar;

        int indiceCorte2 = 0;

//        StringBuffer str = new StringBuffer();
//        str.append(texto);

        while (numeroCarateresMostrar < textoTemporal.length()) {

            int indexDespues  = textoTemporal.indexOf(' ', numeroCarateresMostrar);

            String textoDividido = textoTemporal.substring(0, numeroCarateresMostrar);

            int indexAntes = textoDividido.lastIndexOf(' ');
            int tamPalabraPorCortar = (indexDespues - indexAntes);

            if ((indiceCorte2 = textoDividido.lastIndexOf(' ')) != -1) {
                if(tamPalabraPorCortar <= numeroCarateresMostrar){
                    textoDividido = textoDividido.substring(0, indiceCorte2);
                    indiceCorte = indiceCorte2+1;
                }
            }

            listaTexto.add(textoDividido);

            if (indiceCorte != -1) {
                textoTemporal = textoTemporal.substring(indiceCorte);
            } else {
                textoTemporal = textoTemporal.substring(numeroCarateresMostrar);
            }
        }

        listaTexto.add(textoTemporal);

    }

    public void clear(){
        font = null;
        if( listaTexto != null){
            listaTexto.clear();
            listaTexto = null;
        }
    }
}
